package com.sdkj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdkj.mapper.CheckgroupCheckitemMapper;
import com.sdkj.mapper.CheckgroupMapper;
import com.sdkj.pojo.Checkgroup;
import com.sdkj.pojo.CheckgroupCheckitem;
import com.sdkj.service.CheckgroupService;
import com.sdkj.util.PageResult;
import com.sdkj.util.QueryPageBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CheckgroupServiceImpl implements CheckgroupService {
    @Resource
    private CheckgroupMapper checkgroupMapper;
    @Resource
    private CheckgroupCheckitemMapper checkgroupcheckitemMapper;
    @Override
    public  boolean saveGroupInfo(Integer[] ids, Checkgroup checkgroup){
        boolean flag=checkgroupMapper.insert(checkgroup)>0;
        if (ids!=null&&ids.length>0){
            for (Integer id:ids)
            {
                CheckgroupCheckitem checkgroupCheckitem=new CheckgroupCheckitem();
                checkgroupCheckitem.setCheckgroupId(checkgroup.getId());
                checkgroupCheckitem.setCheckitemId(id);
                flag=checkgroupcheckitemMapper.insert(checkgroupCheckitem)>0;
            }
        }
        return flag;
    }
    @Override
    public PageResult findPageInfo(QueryPageBean queryPageBean)
    {
        String queryString = queryPageBean.getQueryString();
        LambdaQueryWrapper<Checkgroup>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if (queryString!=null && queryString.length()>0){
            lambdaQueryWrapper.like(Checkgroup::getCode,queryString);
            lambdaQueryWrapper.or();
            lambdaQueryWrapper.like(Checkgroup::getName,queryString);
        }
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        Page<Checkgroup>page=new Page<>(currentPage,pageSize);
        Page<Checkgroup> newPage = checkgroupMapper.selectPage(page, lambdaQueryWrapper);

        return new PageResult(newPage.getTotal(),newPage.getRecords());
    }
    //删除检查组信息
    @Override
    public boolean deleteInfoById(Integer id) {
        /*
          删除检查组信息
          1: 删除检查组和检查项关联关系的数据
          2: 检查组的信息
         */
        //删除中间表
        LambdaUpdateWrapper<CheckgroupCheckitem> lambdaUpdateWrapper
                = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(CheckgroupCheckitem::getCheckgroupId,id);
        boolean flag = checkgroupcheckitemMapper.delete(lambdaUpdateWrapper)>0;
        //删除检查组信息
        flag = checkgroupMapper.deleteById(id)>0;
        return flag;
    }

    //根据检查组的id获取多个检查项的id
    @Override
    public List<Integer> getCheckitemids(Integer id) {
        //根据检查组的id查询对应的中间表数据对象
        LambdaQueryWrapper<CheckgroupCheckitem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CheckgroupCheckitem::getCheckgroupId,id);
        List<CheckgroupCheckitem> clist =
                checkgroupcheckitemMapper.selectList(lambdaQueryWrapper);
        //将查询的中间表对象的集合进行遍历 获取每个中间对象包含的检查项 id
        System.out.println(clist);
        List<Integer> list = new ArrayList<>();
        if (clist.size()>0){
            for (CheckgroupCheckitem checkgroupCheckitem : clist) {
                list.add(checkgroupCheckitem.getCheckitemId());
            }
        }
        return list;
    }

    /**
     *
     * @param ids
     * @param checkgroup
     * @return
     */
    @Override
    public boolean updateGroupInfo(Integer[] ids, Checkgroup checkgroup) {
        /**
         * 1: 更新检查组信息
         * 2: 删除该检查组原有的对应信息
         * 3: 将新勾选的关系添加到数据库中
         */

        //更新检查组信息
        boolean flag = checkgroupMapper.updateById(checkgroup)>0;

        //删除中间表
        LambdaUpdateWrapper<CheckgroupCheckitem> lambdaUpdateWrapper
                = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(CheckgroupCheckitem::getCheckgroupId,checkgroup.getId());
        flag = checkgroupcheckitemMapper.delete(lambdaUpdateWrapper)>0;

        //将新勾选的关系添加到数据库中
        if (ids!=null &&ids.length>0){
            for (Integer id : ids) {
                //循环一遍就是一个检查项的id
                CheckgroupCheckitem checkgroupCheckitem = new CheckgroupCheckitem();
                checkgroupCheckitem.setCheckgroupId(checkgroup.getId());
                checkgroupCheckitem.setCheckitemId(id);
                flag = checkgroupcheckitemMapper.insert(checkgroupCheckitem)>0;
            }
        }
        return flag;
    }

    @Override
    public List<Checkgroup> getAllGroupInfo() {
        return checkgroupMapper.selectList(null);
    }
}
